package com.aibaixun.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.aibaixun.common.utils.BeanCopyUtils;
import com.aibaixun.system.domain.dto.BindDeviceDto;
import com.aibaixun.system.domain.vo.QueryDeviceTreeVo;
import com.aibaixun.system.domain.vo.QueryDeviceVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.aibaixun.system.domain.DicmDevice;
import com.aibaixun.system.service.DicmDeviceService;
import com.aibaixun.system.mapper.DicmDeviceMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* @author zhangyh
* @description 针对表【dicm_device(基础设备信息表)】的数据库操作Service实现
* @createDate 2023-09-14 10:17:56
*/
@Service
public class DicmDeviceServiceImpl extends ServiceImpl<DicmDeviceMapper, DicmDevice>
    implements DicmDeviceService{

    @Override
    public List<DicmDevice> getAllDeviceUe() {
        return this.baseMapper.getAllDeviceUe();
    }

    @Override
    public void addDevice(List<DicmDevice> deviceList) {
        deviceList.forEach(item -> {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("device_id",item.getDeviceId());
//            queryWrapper.eq("device_name",item.getDeviceName());
            DicmDevice dicmDevice = baseMapper.selectOne(queryWrapper);
            if (null == dicmDevice){
                baseMapper.insert(item);
            }
        });
    }

    @Override
    public List<QueryDeviceTreeVo> queryDeviceAll(String parentId) {
        return baseMapper.queryDeviceAll(parentId);
    }

    @Override
    public boolean updateDevice(BindDeviceDto dto) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("device_id",dto.getId());
        updateWrapper.set("ue_id",dto.getUeId());
        int update = baseMapper.update(null, updateWrapper);
        if (update == 1){
            return true;
        }
        return false;
    }

    @Override
    public void updateDeviceUeId(BindDeviceDto dto) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("address_id",dto.getId());
        List<DicmDevice> deviceList = baseMapper.selectList(queryWrapper);
        if (CollectionUtil.isNotEmpty(deviceList)){
            deviceList.forEach(item -> {
                if (StringUtils.isNotEmpty(item.getUeId())){
                    String ueId = "";
                    String[] split = item.getUeId().split("-");
                    for (int i = 0; i < split.length; i++) {
                        if (i < 3){
                            ueId = ueId+split[i]+"-";
                        }else if (i == 3){
                            ueId = ueId+split[i];
                        }
                    }

                    if (StringUtils.isNotEmpty(ueId)){
                        String newUeId = item.getUeId().replace(ueId, dto.getUeId());
                        item.setUeId(newUeId);
                    }
                }else {
                    item.setUeId(dto.getUeId()+"-"+item.getDeviceName());
                }
                baseMapper.updateById(item);
            });
        }
    }

    @Override
    public QueryDeviceVo queryByRoomUeId(String cabinetId) {
        return baseMapper.queryByRoomUeId(cabinetId);
    }

    @Override
    public void resetUeId() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("ue_id","空");
        List<DicmDevice> deviceList = baseMapper.selectList(queryWrapper);
        if (CollectionUtil.isNotEmpty(deviceList)){
            deviceList.forEach(item -> {
                item.setUeId("");
                baseMapper.updateById(item);
            });
        }
    }
}




